/**
 * @see 该页面是查询成绩的页面
 */
import {
	getXnxqListGxdx,
	getXnxqList,
	getPageAdPicList,
	handleAdPicClick,
} from "../../../utils/util"
import { API_CONFIG } from "../../../config"
import { request } from "../../../plugins/api"
let xnxqList = []

Page({
	data: {
		index: xnxqList.length - 1,
		xnxqList,
		userInfo: "", // 用户信息
		bgStyle: `background-image: url("${API_CONFIG.staticUrl}wx-x-mai/bg.jpg")`, // 背景图片
		sTotalCredits: "", // 在校总学分
		sYearAverage: "", // 历年平均分
		sTermCredits: "", // 学期总学分
		sTermAverage: "", // 学期平均分
		aGradeListData: [], // 查询成绩数据
		appMsg: {
			type: "", // 状态
			show: false, // 是否展示
		},
		BannerList: [], //轮播列表
		TopOrdersGetList: [], //下单列表
	},
	onLoad() {
		let schoolConfig = wx.getStorageSync("schoolConfig")
		xnxqList =
			schoolConfig.schoolCode + "" === "4145010593"
				? getXnxqListGxdx()
				: getXnxqList()
		this.setData({
			userInfo: wx.getStorageSync("userInfo"),
			xnxqList,
			index: xnxqList.length - 1,
		})
		this.gradeInfo()
		this.getList()
		this.getTopOrdersGet()
	},
	//下单记录列表
	getTopOrdersGet() {
		this.setData({
			appMsg: {
				type: "loading",
				show: true,
			},
		})
		const request = {
			schoolCode: wx.getStorageSync("schoolConfig").schoolCode,
			userId: wx.getStorageSync("userInfo").userId,
			size: 30,
		}
		wx.$API["schoolStudy/topOrdersGet"](request)
			.then((response) => {
				this.setData({
					appMsg: {
						type: "loading",
						show: false,
					},
				})
				const { data } = response
				this.setData({
					TopOrdersGetList: data,
				})
			})
			.catch((error) => {
				this.setData({
					appMsg: {
						type: "err",
						show: true,
					},
				})
			})
	},
	//获取轮播图
	getList() {
		this.setData({
			appMsg: {
				type: "loading",
				show: true,
			},
		})
		getPageAdPicList("schoolStudy:grade").then((list) => {
			this.setData({
				appMsg: {
					type: "loading",
					show: false,
				},
			})
			console.log(list, "list")
			this.setData({
				BannerList: list,
			})
		})
		// wx.$API['schoolStudy/getEnableBannerList']()
		// 	.then((response) => {
		// 		this.setData({
		// 			appMsg: {
		// 				type: 'loading',
		// 				show: false,
		// 			},
		// 		})
		// 		const { data } = response
		// 		data.forEach((item) => {
		// 			item.imageUrl = API_CONFIG.staticUrl + item.imageUrl
		// 		})
		// 		this.setData({
		// 			BannerList: data,
		// 		})
		// 	})
		// 	.catch((error) => {
		// 		this.setData({
		// 			appMsg: {
		// 				type: 'err',
		// 				show: true,
		// 			},
		// 		})
		// 	})
	},
	//  点击swiper-item
	onTapSwiper(e) {
		let item = e.currentTarget.dataset.item
		handleAdPicClick(item)

		// wx.$API["schoolStudy/addBannerHti"]({
		// 	bannerId: item.id,
		// 	bannerRequestUrl: item.requestUrl,
		// })
		// 	.then((response) => {
		// 		this.setData({
		// 			appMsg: {
		// 				type: "loading",
		// 				show: false,
		// 			},
		// 		})
		// 	})
		// 	.catch((error) => {
		// 		this.setData({
		// 			appMsg: {
		// 				type: "err",
		// 				show: true,
		// 			},
		// 		})
		// 	})

		// if (!item.requestUrl) {
		// 	// wx.showToast({
		// 	//   title: '该项未配置跳转地址,请联系相关管理员配置',
		// 	//   icon: 'none',
		// 	// })
		// 	return
		// }
		//
		// if (item.appid) {
		// 	wx.navigateToMiniProgram({
		// 		appId: item.appid,
		// 		path: item.requestUrl,
		// 		envVersion: item.envVersion,
		// 		extraData: {
		// 			from: "xwx",
		// 			schoolCode: wx.getStorageSync("schoolConfig").schoolCode,
		// 			userId: wx.getStorageSync("userInfo").userId,
		// 		},
		// 	})
		// } else if (/^http(s)?/.test(item.requestUrl)) {
		// 	wx.navigateTo({
		// 		url: `/pages/web/web?path=${encodeURIComponent(item.requestUrl)}`,
		// 	})
		// } else {
		// 	wx.navigateTo({
		// 		url: item.requestUrl,
		// 	})
		// }
	},
	openMall() {
		wx.navigateTo({
			url: "/pages/web/web?path=https://xwx.gzzmedu.com/file-agency/", // 文件代办
			// url: '/pages/web/web?path=https://xwx.gzzmedu.com/xwx-mall', // 商城模块
		})
	},
	/**
	 * @see 获取查询成绩列表
	 */
	gradeInfo() {
		const { xnxqList, index } = this.data
		const params = {
			schoolYear: xnxqList[index].id,
		}
		this.setData({
			appMsg: {
				type: "loading",
				show: true,
			},
		})
		request({
			url: "api/school-service-ms/user/studentScore",
			data: params,
		})
			.then((response) => {
				this.setData({
					appMsg: {
						type: "loading",
						show: false,
					},
				})
				const data = response.data
				const dataLength = response.data.length
				if (dataLength === 0) {
					this.setData({
						appMsg: {
							type: "none",
							show: true,
						},
					})
				} else {
					this.setData({
						sTotalCredits: data[0].totalCredits,
						sYearAverage: data[0].yearAverage,
						sTermCredits: data[0].termCredits,
						sTermAverage: data[0].termAverage,
						aGradeListData: data,
					})
				}
			})
			.catch((error) => {
				this.setData({
					appMsg: {
						type: "err",
						show: true,
					},
				})
				wx.showToast({
					title: error,
					icon: "none",
				})
				console.log(error, "error")
			})
	},
	/**
	 * @see 点击刷新页面
	 */
	clickMsgButton() {
		this.gradeInfo()
	},
	/**
	 * @see 点击选择下拉学年学期
	 */
	bindPickerChange(e) {
		this.setData({
			index: e.detail.value,
		})
		this.gradeInfo()
	},
	onShareAppMessage() {
		return {
			path: "/pages/home/index",
		}
	},
})
